window.onload = function () {
  document.querySelector('#login-btn').addEventListener('click', function () {
    let phone = document.querySelector('#phone').value;
    let password = document.querySelector('#password').value;
    login(phone, password);
  })
  document.querySelector('#register').addEventListener('click', function () {
    window.location.href = "./regist.html";
  })
}

let login = async (phone, password) => {
  let result = await ajax({
    type: 'POST',
    url: 'http://127.0.0.1:4000/logindl/logins',
    data: { phone, password }
  });
  let res = JSON.parse(result);
  // console.log(res);
  if (res.code == 200) {
   
    let students_mes = res.data[0].phone
    let students_mes_id = res.data[0]._id
    // localStorage.setItem('token', res.token);
    // localStorage.getItem('phone');
    window.location.href = `homepage.html?${students_mes}=${students_mes_id}`;
  } else if (res.code == 201) {
    alert(res.message);
  }
}

// const jwt = require('jsonwebtoken');
// // const { TOKEN_KEY } = require('../until/consts');
// router.post('/login', async (req, res) => {
//   let data = await userModel.find({ username: req.body.username, password: req.body.password })
//   if (data.length > 0) {
//     // const token = jwt.sign(
//     //   { user: data[0]},
//     //   TOKEN_KEY,
//     //   { expiresIn: 10}
//     // );
//     res.send({
//       code: 200,
//       message: '登录成功',
//       token: 'Bearer ' + token
//     })
//   } else {
//     res.send({
//       code: 201,
//       message: '账号或密码错误'
//     })
//   }
// })
